var storage = storages.create("autotouchShijieXy");
var delayBase = 1;
var shibiefangshi = 1;
var yanchi = 500; // 点击后停顿
var yanchi1 = 1000; // 点击前停顿
var yanchi2 = 2000; // 点击后停顿
var yanchi3 = 3000; // 点击后停顿
var yanchi5 = 5000; // 点击后停顿
var deviceWidth = device.width > device.height ? device.width : device.height;
var deviceHeight = device.height > device.width ? device.width : device.height;
var picJizhun = deviceHeight / 1080;
var picJizhunHeight = deviceHeight / 1080;
var duiwushangxian = Boolean(storage.get("tianxuan", true)) ? 3 : 2;
var shangciweizhi = storage.get("shangciweizhi", { x: 0, y: 0 }); // 上次位置坐标
var xiaodituPosit = storage.get("xiaodituPosit", { x: 0, y: 0 }); // 小地图起始位置
var yuanbaoweizhi = storage.get("yuanbaoweizhi", { x: 2020, y: 0 }); // 元宝位置
var zhandouliWeizhi = storage.get("zhandouliPosit", { x: 0, y: 0, w: 500, h: 65 }); // 战斗力起始位置
var cunliangWeizhi = storage.get("cunliangWeizhi", { x: 0, y: 0, w: 260, h: 45 }); // 存量起始位置
var yingxiongMsgWeizhi = storage.get("yingxiongMsgWeizhi", { x: 0, y: 0, w: 300, h: 0 }); // 英雄信息坐标
var ziyuanZuobianWeizhi = storage.get("ziyuanZuobianWeizhi", { x: 0, y: 0, w: 300, h: 65 }); // 资源信息坐标 

function myLogFunc() {
    log(arguments)
}

if (deviceHeight == 720) {
    picJizhun = 0.666666666666667;
}
if (deviceHeight == 1200) {
    picJizhun = 1.12;
}
if (deviceHeight == 1220) {
    picJizhun = 1.1257;
}
if (deviceHeight == 1240) {
    picJizhun = 1.133;
}
picJizhunHeight = picJizhun;
// 后行
if (deviceHeight == 1152) { // 2376
    picJizhun = 1.0658; // 1.0666666666666667 // 1.093859999999997
    picJizhunHeight = 1.04471;
}

log("基准",picJizhun)

// let thisImgs3 = images.read("./res/臣.png");
//                 thisImgs3 = images.scale(thisImgs3, 1, 1.1)
//                 let p3 = findImage(captureScreen(), thisImgs3, { threshold: 0.8 });
//                 thisImgs3.recycle();
//                 if (p3) {
//                     log('DFDFDF')
//                 } else {
//                     log('没识别')
//                 }


// let thisImgs4 = images.read("./res/训练.png");
//                     thisImgs4 = images.scale(thisImgs4, picJizhun, picJizhunHeight)
//                     let p4 = findImage(captureScreen(), thisImgs4, { threshold: 0.75 });
//                     thisImgs4.recycle();
//                     if (p4) {
//                         myLogFunc("训练步兵")

//                     } else {
//                         myLogFunc("不了训练")

//                     }


// 测试分辨率基准问题
console.log('picJizhun',picJizhun)
picJizhun = 1.11
let enter = true;
while (enter) {
    let thisImgs = images.read("./res/军报.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhun)
    let p = findImage(captureScreen(), thisImgs,{ threshold: 0.96 });
    thisImgs.recycle();
    if (p) {
        log('又要呦呦哟与哦与哦与欧亚', picJizhun)
        enter = false;
        sleep(yanchi1)
    } else {
        log('nonono', picJizhun)
        picJizhun += 0.003
        sleep(yanchi)
        if (picJizhun > 1.19) {
            log('极限结束')
            enter = false;
        }
    }
}


// 1.162

// 1.1309999999999993 1.1329999999999991
// 1.1324999999999992
// 1.1349999999999993


// 自动化准备
// auto.waitFor()
// requestScreenCapture(true)


// let cap = captureScreen();
// let threshold = 0.83;
// let thisImgs = images.read("./res/一级野.png");
// thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight);
// let thisResult = images.matchTemplate(cap, thisImgs, { threshold: threshold, max: 6 });
// thisImgs.recycle();
// if (thisResult.matches.length) {
//     log('thisResult1', thisResult)
//     // let points = thisResult.sortBy("left-right").points;
//     // pointsAllList = pointsAllList.concat(points);
//     // log('pointsAllList1', pointsAllList)
// }else{
//     console.log('nononono')
// }





// test()
function test() {
    let cap = captureScreen()
    log(1)
    myLogFunc('没有采集标志武将')
            myLogFunc('进入比分采集')
            let thisImgsElse = images.read("./res/英雄信息.png");
            thisImgsElse = images.scale(thisImgsElse, picJizhun, picJizhunHeight)
            let result = images.matchTemplate(cap, thisImgsElse, { region: [0, (deviceHeight / 2), deviceWidth, (deviceHeight / 2)], threshold: 0.5, max: 8 });
            thisImgsElse.recycle();
            if (result.matches.length) {
                let bestMatch = {}
                let bestNumber = 0;
                result.sortBy("left-right").matches.forEach((match, indes) => {
                    if (indes >= storage.get("caijibaoliuHero", "0")) {
                        click(match.point.x - 109, match.point.y + 168.5);
                        sleep(yanchi1)
                        // 如果没有规划读取资源数据区域
                        if (yingxiongMsgWeizhi.x == 0) {
                            myLogFunc("没有配置采集数据区域")
                            let thisImgs1 = images.read("./res/资源负重.png");
                            thisImgs1 = images.scale(thisImgs1, picJizhun, picJizhunHeight)
                            let thisImgs2 = images.read("./res/资源耗时.png");
                            thisImgs2 = images.scale(thisImgs2, picJizhun, picJizhunHeight)
                            let p1 = findImage(cap, thisImgs1, { region: [(deviceWidth / 2), 0, (deviceWidth / 2), deviceHeight], threshold: 0.8 });
                            let p2 = findImage(cap, thisImgs2, { region: [(deviceWidth / 2), 0, (deviceWidth / 2), deviceHeight], threshold: 0.8 });
                            yingxiongMsgWeizhi.x = p1.x
                            yingxiongMsgWeizhi.y = p1.y
                            yingxiongMsgWeizhi.h = p2.y - p1.y
                            myLogFunc('采集数据区域', yingxiongMsgWeizhi)
                            storage.put("yingxiongMsgWeizhi", yingxiongMsgWeizhi);
                            yingxiongMsgWeizhi = storage.get("yingxiongMsgWeizhi", { x: 0, y: 0, w: 300, h: 0 }); // 英雄信息坐标
                        }

                        // 查看采集分和负重分做比较
                        let clip = images.clip(captureScreen(), yingxiongMsgWeizhi.x, yingxiongMsgWeizhi.y, yingxiongMsgWeizhi.w, yingxiongMsgWeizhi.h);
                        let orcRes = ""
                        let fraction = 0

                        if (shibiefangshi == 0) {
                            orcRes = paddle.ocrText(clip);
                            fraction = orcRes[1].split('：')[1]
                            fraction = parseFloat(fraction)
                        } else if (shibiefangshi == 1) {
                            orcRes = paddle.ocrText(clip);
                            fraction = orcRes[1].split('：')[1]
                            fraction = parseFloat(fraction)
                        } else {
                            orcRes = gmlkit.ocr(clip, "zh");
                            if (orcRes.children.length > 1) {
                                fraction = orcRes.children[1].text.split(': ')[1]
                                fraction = parseFloat(fraction)
                            }
                        }
                        clip.recycle();
                        // 记录最大分和英雄位置
                        if (fraction > bestNumber) {
                            bestNumber = fraction;
                            bestMatch = match;
                        }
                    } else {
                        myLogFunc('跳过保留英雄')
                    }
                    sleep(yanchi)
                });

                sleep(yanchi)
                myLogFunc('采集分最高：' + bestNumber)
                if (bestMatch.point) {
                    click(bestMatch.point.x - 109, bestMatch.point.y + 168.5);
                    sleep(yanchi1)
                }

                let thisImgsRc = images.read("./res/资源采集_no.png");
                thisImgsRc = images.scale(thisImgsRc, picJizhun, picJizhunHeight)
                let p = findImage(captureScreen(), thisImgsRc, { region: [0, 0, (deviceWidth / 2), (deviceHeight / 2)] });
                thisImgsRc.recycle();
                if (p) {
                    myLogFunc('兵力不足')
                    let matchRc = result.sortBy("left-right").matches;
                    let isChengLe = false;
                    for (let index = 0; index < matchRc.length; index++) {
                        click(matchRc[index].point.x - 109, matchRc[index].point.y + 168.5);
                        sleep(yanchi1)
                        if (caijiRcFunc()) {
                            log('备用采集英雄')
                            isChengLe = true;
                            break;
                        }
                    }
                    if (!isChengLe) {
                        xiaobingbuzu = true;
                        myLogFunc('全体兵种不足')
                        back()
                        sleep(yanchi1)
                        back()
                        sleep(yanchi)
                    }
                }
            } else {
                myLogFunc('界面异常！')
                chaoguocaiFunc()
            }
}










// 查看世界地图
function seeShijieditu() {
    let thisImgs = images.read("./res/世界地图按钮.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs);
    thisImgs.recycle();
    if (p) {
        myLogFunc('打开世界地图')
        click(p.x, p.y)
        sleep(yanchi1)

        // 如果是第一次则配置地图 700*600
        if (xiaodituPosit.x == 0) {
            myLogFunc('第一次配置小地图参数')
            sleep(yanchi1)
            let thisImgs1 = null;
            // if (Boolean(storage.get("isSanguo", false))) {
            //     thisImgs1 = images.read("./res/小地图1.png");
            // }else{
            //     thisImgs1 = images.read("./res/小地图.png");
            // }
            thisImgs1 = images.read("./res/小地图1.png");
            thisImgs1 = images.scale(thisImgs1, picJizhun, picJizhunHeight)
            let p1 = findImage(captureScreen(), thisImgs1, { threshold: 0.9 });
            thisImgs1.recycle();
            if (p1) {
                log('小地图起点坐标', p1)
                let newObj = { x: p1.x, y: p1.y };
                storage.put("xiaodituPosit", newObj);
                xiaodituPosit = storage.get("xiaodituPosit", { x: 0, y: 0 });
            } else {
                toastLog("小地图信息异常")
                exit()
            }
        }
    } else {
        myLogFunc('打开地图失败')
        sleep(yanchi)
    }
}

// 前往指定坐标 483就是16 进阶2.82 需要算法支持各种分辨率
function goXyFunc(x, y) {
    myLogFunc('前往坐标点', x, y)
    x = Number(x)
    y = Number(y)
    let zhia = parseInt(x / 50) + 1;
    let jiaozheng = 3;
    if (x < 127) {
        jiaozheng = 2;
        //     zhia = zhia - 1;
    }
    let picJizhunYC = picJizhun;
    let picJizhunHeightYC = picJizhunHeight;
    if (picJizhun == 1.133) {
        picJizhunYC = 1.147;
        picJizhunHeightYC = 1.147;
    }
    let xx = (xiaodituPosit.x + jiaozheng) + ((x + zhia) * (2.754 * picJizhunYC))
    let yy = (xiaodituPosit.y + 0.5) + (y * (2.4 * picJizhunHeightYC))
    if (Boolean(storage.get("isSanguo", false))) {
        xx = (xiaodituPosit.x) + ((x) * (4.328 * picJizhunYC))
        yy = (xiaodituPosit.y) + (y * (3.652 * picJizhunHeightYC))
    }
    log('小地图屏幕点：', xx, yy)
    sleep(yanchi1)
    click(xx, yy)
    sleep(yanchi2)
}
